local which_key = require('which-key')


-- telescope ui
local function code_actions()
  local opts = {
    winblend = 15,
    layout_config = {
      prompt_position = "top",
      width = 80,
      height = 12,
    },
    borderchars = {
      prompt = { "─", "│", " ", "│", "╭", "╮", "│", "│" },
      results = { "─", "│", "─", "│", "├", "┤", "╯", "╰" },
      preview = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
    },
    border = {},
    previewer = false,
    shorten_path = false,
  }
  require("telescope.builtin").lsp_code_actions(require("telescope.themes").get_dropdown(opts))
end

-- 基础设置
vim.g.mapleader = " "
vim.api.nvim_set_keymap('i', 'jk','<ESC>', {noremap = true, silent=true})
vim.api.nvim_set_keymap('n', '<Esc>','<cmd>nohlsearch<cr>', {noremap = true, silent=true})
vim.api.nvim_set_keymap('n', '<S-l>','<cmd>BufferNext<cr>', {noremap = true, silent=true})
vim.api.nvim_set_keymap('n', '<S-h>','<cmd>BufferPrevious<cr>', {noremap = true, silent=true})
vim.api.nvim_set_keymap('t', '<Esc>','<cmd>ToggleTerm<cr>', {noremap = true, silent=true})

vim.api.nvim_set_keymap('n', '<','<<', {noremap = true, silent=true})
vim.api.nvim_set_keymap('n', '>','>>', {noremap = true, silent=true})
vim.api.nvim_set_keymap('v', '<','< | gv', {noremap = true, silent=true})
vim.api.nvim_set_keymap('v', '>','> | gv', {noremap = true, silent=true})

-- auto format (目前有bug，0.6.x以上版本遇到问题)
-- vim.cmd [[au BufWritePre * lua vim.lsp.buf.formatting_sync()]]

-- which-key
which_key.setup {}
which_key.register({
  ['/'] = { ':CommentToggle<cr>', '注释' },

  q = { '<cmd>q<cr>', '退出' },

  w = {
    name = "Window",
    h = { '<C-w>h', 'left' },
    j = { '<C-w>j', 'bottom' },
    k = { '<C-w>k', 'top' },
    l = { '<C-w>l', 'right' },
  },

  f = {
    name = "文件相关",
    s = { '<cmd>w<cr>', '保存文件' },
  },

  e = { '<cmd>NvimTreeToggle<cr>', '文件管理' },

  c = { '<cmd>BufferClose<cr>','关闭当前缓冲区' },

  b = {
    name = "缓冲区",
    b = { '<cmd>Telescope buffers<cr>', '缓冲区列表' },
    o = { '<cmd>BufferCloseAllButCurrent<cr>', '关闭其它缓冲区' },
  },

  s = {
    name = "搜索相关",
    f = { '<cmd>lua require("telescope.builtin").find_files()<cr>', '搜索文件' },
    b = { '<cmd>lua require("telescope.builtin").buffers()<cr>', '搜索buffer' },
    l = { '<cmd>lua require("telescope.builtin").current_buffer_fuzzy_find()<cr>', '模糊搜索当前' },
    o = { '<cmd>lua require("telescope.builtin").oldfiles()<cr>', '历史文件' },
    c = { '<cmd>lua require("telescope.builtin").live_grep()<cr>', '搜索代码' },
    p = { '<cmd>Telescope projects<cr>', '项目历史' },
  },

  -- d = {
  --   name = "Debug",
  --   t = {'<cmd>lua require("dap").toggle_breakpoint()<cr>', '开/关断点'},
  --   b = {'<cmd>lua require("dap").step_back()<cr>', '步回'},
  --   i = {'<cmd>lua require("dap").step_into()<cr>', '单步'},
  --   o = {'<cmd>lua require("dap").step_over()<cr>', '单步(跳过)'},
  --   u = {'<cmd>lua require("dap").step_out()<cr>', '步回'},
  --   s = {'<cmd>lua require("dap").continue()<cr>', '启动'},
  --   q = {'<cmd>lua require("dap").close()<cr>', '退出'},
  -- },

  g = {
    name = "Git",
    j = { '<cmd>lua require("gitsigns").next_hunk()<cr>', '下一区块' },
    k = { '<cmd>lua require("gitsigns").prev_hunk()<cr>', '上一区块' },
    s = { '<cmd>lua require("gitsigns").stage_hunk()<cr>', '暂存区块' },
    p = { '<cmd>lua require("gitsigns").preview_hunk()<cr>', '预览区块' },
    u = { '<cmd>lua require("gitsigns").undo_stage_hunk()<cr>', '取消暂存' },
    r = { '<cmd>lua require("gitsigns").reset_hunk()<cr>', '重置区块' },
    R = { '<cmd>lua require("gitsigns").reset_buffer()<cr>', '重置缓存' },
    o = { '<cmd>Telescope git_status<cr>', '打开修改后文件' },
    b = { '<cmd>Telescope git_branches<cr>', '切换分支' },
    c = { '<cmd>Telescope git_commits<cr>', '切换提交历史' },
  },

  l = {
    name = "Lsp",
    q = { '<cmd>Telescope lsp_document_symbols<cr>', '全局变量' },
    f = { '<cmd>lua vim.lsp.buf.formatting()<cr>', '格式化文件' },
    e = { '<cmd>Telescope lsp_document_diagnostics<cr>', '诊断' },
    w = { '<cmd>Telescope lsp_workspace_diagnostics<cr>', '项目诊断' },
    r = { '<cmd>lua vim.lsp.buf.rename()<cr>', '重命名' },
    d = { '<cmd>lua vim.lsp.buf.definition()<cr>', '查看代码定义' },
    h = { '<cmd>lua vim.lsp.buf.hover()<cr>', '查看注释' },
    t = { '<cmd>SymbolsOutline<cr>', '查看tag' },
    a = { '<cmd>lua require("mapping").code_actions()<cr>', 'Actions' },
    l = { '<cmd>TroubleToggle<cr>', 'trouble' },
  },

  t = {'<cmd>ToggleTerm<cr>', 'terminal'},
}, { prefix = '<leader>', mode = 'n', noremap = true, silent = true })

which_key.register({
  ['/'] = { ':CommentToggle<cr>', '注释' },
}, { prefix = '<leader>', mode = 'v', noremap = true, silent = true })

return {
  code_actions = code_actions
}
